package com.ddwl.wallet.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ddwl.common.config.DingTalkNotify;
import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.common.feign.api.OrderApi;
import com.ddwl.schema.vo.funds.WxEntPayVo;
import com.ddwl.schema.vo.order.OrderInfoVo;
import com.ddwl.wallet.service.DistOrderInfoService;
import com.ddwl.wallet.service.DistOrderService;
import com.ddwl.wallet.service.OrderPayOffService;
import com.ddwl.wallet.service.WithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 类RabbitMqListener.java的实现描述：消息队列
 *
 * @author zach
 * @Date :2019-05-08 14:55.
 */
@Component
@Slf4j
public class WalletMqMessageReceiver {

    @Resource
    private DistOrderService distOrderService;
    @Resource
    private DingTalkNotify dingTalkNotify;
    @Resource
    private OrderPayOffService orderPayOffService;
    @Resource
    private DistOrderInfoService distOrderInfoService;
    @Resource
    private WithdrawService withdrawService;

    @Resource
    private OrderApi orderApi;
    /**
     * 结算订单分销佣金
     * @param orderId
     */
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.DIST_DIST_ORDER_SETTLEMENT_AMOUNT_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.DIST_DIST_ORDER_SETTLEMENT_AMOUNT_QUEUE)})
    public void payOffShopCommission(String orderId){
        log.info("开始结算订单分销佣金 orderNo={}",orderId);
        try {
            orderPayOffService.payOffDistCommission(orderId);
        }catch(Exception e){
            log.error( "订单结算店铺订单佣金异常 orderId={}", orderId,e);
            dingTalkNotify.exceptionNotify(e);
        }

    }

    /**
     * 结算商户订单铺佣金
     * @param orderId
     */
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.DIST_SHOP_ORDER_SETTLEMENT_AMOUNT_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.DIST_SHOP_ORDER_SETTLEMENT_AMOUNT_QUEUE)})
    public void payOffDistCommission(String orderId){
        log.info("开始结算商户订单铺佣金 orderNo={}",orderId);
        try {
            orderPayOffService.payOffShopCommission(orderId);
        }catch(Exception e){
            log.error( "结算商户订单铺佣金异常 orderId={}", orderId,e);
            dingTalkNotify.exceptionNotify(e);
        }
    }


    @RabbitListener(queues = RabbitMqConstants.DIST_ORDER_CALCULATE_QUEUE)
    public void orderDistCalculate(String order) {
        log.info("开始订单分销金额计算 orderNo={}",order);
        try {
            distOrderInfoService.syncInfo(order);
        }catch (Exception e){
            log.error( "订单分销金额计算异常 orderId={}", JSON.parseObject(order).getString("id"),e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.DIST_SHOP_ORDER_CALCULATE_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.DIST_SHOP_ORDER_CALCULATE_QUEUE)})
    public void shopOrderDistCalculate(String order) {
        log.info("开店订单分销金额计算 orderNo={}",order);
        try {
            distOrderInfoService.syncInfo(order);
        }catch (Exception e){
            log.error( "开店订单分销金额计算异常 order ={} ",order,e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.DIST_ORDER_CONFIRM_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.DIST_ORDER_CONFIRM_QUEUE)})
    public void orderDistFinish(String orderNo) {
        log.info("开始同步订单确认收货消息 orderNo={}",orderNo);
        try {
            OrderInfoVo order = orderApi.getOrderInfo(orderNo).getResult();
            if(order!=null){
                distOrderInfoService.syncInfo(JSON.toJSONString(order));
            }
        }catch (Exception e){
            log.error( "开始同步订单确认收货消息异常 orderId={}", orderNo,e);
            dingTalkNotify.exceptionNotify(e);
        }
    }


    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.RECORD_VIEWS_COUNT,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.RECORD_VIEWS_COUNT)})
    public void orderViewsCount(String source) {
        log.info("开始记录浏览量 source={}",source);
        try {
            distOrderService.recordViewsCount(source);
        }catch (Exception e){
            log.error( "记录浏览量消息异常 source = {}",source,e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = RabbitMqConstants.WX_ENT_PAY_QUEUE, durable = "true"),
            exchange = @Exchange(value = RabbitMqConstants.WX_ENT_PAY_EXCHANGE), key = RabbitMqConstants.WX_ENT_PAY_QUEUE)})
    public void withdrawResultNotify(String result) {
        log.info("提现通知处理 result={}",result);
        try {
            WxEntPayVo wxEntPayVo = JSONObject.parseObject(result, WxEntPayVo.class);
            withdrawService.dealWithNotify(wxEntPayVo);
        } catch (Exception e) {
            log.error( "提现通知处理异常",e);
            dingTalkNotify.exceptionNotify(e);
        }
    }

}
